/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paquetevistas.configuracion;

import produccion.controlador.ControladorProduccion;
import javax.swing.JOptionPane;

/**
 *
 * @author James
 */
public class ParametrosAlgoritmos extends javax.swing.JInternalFrame {
    private modelos.Parametrosalgoritmo parametro;
    /**
     * Creates new form ParametrosAlgoritmos
     */
    public ParametrosAlgoritmos() {
        initComponents();
        parametro=ControladorProduccion.seleccionarParametroAlgoritmo();
        cargarDatos();
    }

    private void cargarDatos() {
        txtAlpha.setText("" + parametro.getAlfa());
        txtNumeroIteraciones.setText("" + parametro.getNumiteraciones());
        txtMaxCiclosSinCambiar.setText("" + parametro.getMaxciclossincambiar());
        txtMaximoPoblacion.setText("" + parametro.getTammaxpoblacion());
        txtTasaDeMutacion.setText("" + parametro.getTasamutacion());
        txtMinimoPoblacion.setText("" + parametro.getTampoblacioninicial());
        txtTasaDeCasamiento.setText("" + parametro.getTasacasamiento());
        txtNumeroDeHilos.setText("" + parametro.getNumhilos());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGuardar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtAlpha = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNumeroIteraciones = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtMaxCiclosSinCambiar = new javax.swing.JTextField();
        txtMaximoPoblacion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtTasaDeMutacion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtMinimoPoblacion = new javax.swing.JTextField();
        txtTasaDeCasamiento = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtNumeroDeHilos = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Configuracion de Algoritmos");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/save.png"))); // NOI18N
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Grasp"));

        jLabel3.setText("Alpha:");

        jLabel4.setText("Iteraciones:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(89, 89, 89)
                .addComponent(txtAlpha, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(53, 53, 53)
                .addComponent(txtNumeroIteraciones, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(11, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAlpha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtNumeroIteraciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(9, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Genético"));

        jLabel5.setText("Ciclos sin cambiar:");

        jLabel6.setText("Maximo Poblacion:");

        jLabel9.setText("Tasa de Mutacion:");

        jLabel7.setText("Poblacion inicial:");

        jLabel8.setText("Tasa de Casamiento:");

        jLabel10.setText("Numero de Hilos:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtMaximoPoblacion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                    .addComponent(txtMaxCiclosSinCambiar, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTasaDeMutacion))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(txtNumeroDeHilos, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMinimoPoblacion, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                            .addComponent(txtTasaDeCasamiento))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtMaxCiclosSinCambiar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtMinimoPoblacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtMaximoPoblacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtTasaDeMutacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtNumeroDeHilos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtTasaDeCasamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/error 1.png"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(10, 10, 10))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if (!txtAlpha.getText().equals("") && !txtMaxCiclosSinCambiar.getText().equals("")
                && !txtMaximoPoblacion.getText().equals("") && !txtMinimoPoblacion.getText().equals("")
                && !txtNumeroDeHilos.getText().equals("") && !txtNumeroIteraciones.getText().equals("")
                && !txtTasaDeCasamiento.getText().equals("") && !txtTasaDeMutacion.getText().equals("") && isInteger(txtMaxCiclosSinCambiar.getText())
                && isInteger(txtMaximoPoblacion.getText()) && isInteger(txtMinimoPoblacion.getText())
                && isInteger(txtNumeroDeHilos.getText()) && isInteger(txtNumeroIteraciones.getText())
                && isNumeric(txtAlpha.getText()) && isNumeric(txtTasaDeCasamiento.getText()) && isNumeric(txtTasaDeMutacion.getText())
                && Double.parseDouble(txtAlpha.getText()) >= 0 && Double.parseDouble(txtAlpha.getText()) <= 1
                && Double.parseDouble(txtTasaDeCasamiento.getText()) >= 0 && Double.parseDouble(txtTasaDeCasamiento.getText()) <= 1
                && Double.parseDouble(txtTasaDeMutacion.getText()) >= 0 && Double.parseDouble(txtTasaDeMutacion.getText()) <= 1
                && Integer.parseInt(txtMaxCiclosSinCambiar.getText()) >= 0 && Integer.parseInt(txtMaximoPoblacion.getText()) >= 0
                && Integer.parseInt(txtMinimoPoblacion.getText()) >= 0 && Integer.parseInt(txtNumeroDeHilos.getText()) > 0
                && Integer.parseInt(txtNumeroIteraciones.getText()) >= 0 && Integer.parseInt(txtMaximoPoblacion.getText()) > Integer.parseInt(txtMinimoPoblacion.getText())) {
            guardar();
        } else {
            String error = "";
            if (!txtNumeroIteraciones.getText().equals("")) {
                if (isInteger(txtNumeroIteraciones.getText())) {
                    if (Integer.parseInt(txtNumeroIteraciones.getText()) < 0) {
                        error += "Numero de Iteraciones, ";
                    }
                } else {
                    error += "Numero de Iteraciones, ";
                }
            } else {
                error += "Numero de Iteraciones, ";
            }
            if (!txtAlpha.getText().equals("")) {
                if (isNumeric(txtAlpha.getText())) {
                    if (Double.parseDouble(txtAlpha.getText()) < 0 || Double.parseDouble(txtAlpha.getText()) > 1) {
                        error += "alpha, ";
                    }
                } else {
                    error += "alpha, ";
                }
            } else {
                error += "alpha, ";
            }
            if (!txtMaxCiclosSinCambiar.getText().equals("")) {
                if (isInteger(txtMaxCiclosSinCambiar.getText())) {
                    if (Integer.parseInt(txtMaxCiclosSinCambiar.getText()) < 0) {
                        error += "Max Ciclos sin Cambiar, ";
                    }
                } else {
                    error += "Max Ciclos sin Cambiar, ";
                }
            } else {
                error += "Max Ciclos sin Cambiar, ";
            }
            if (!txtMaximoPoblacion.getText().equals("")) {
                if (isInteger(txtMaximoPoblacion.getText())) {
                    if (Integer.parseInt(txtMaximoPoblacion.getText()) < 0) {
                        error += "Maximo de Poblacion, ";
                    } else if (!txtMinimoPoblacion.getText().equals("")) {
                        if (isInteger(txtMinimoPoblacion.getText())) {
                            if (Integer.parseInt(txtMinimoPoblacion.getText()) > Integer.parseInt(txtMaximoPoblacion.getText())) {
                                error += "Maximo de Poblacion, ";
                            }
                        }
                    }
                } else {
                    error += "Maximo de Poblacion, ";
                }
            } else {
                error += "Maximo de Poblacion, ";
            }
            if (!txtMinimoPoblacion.getText().equals("")) {
                if (isInteger(txtMinimoPoblacion.getText())) {
                    if (Integer.parseInt(txtMinimoPoblacion.getText()) < 0) {
                        error += "Minimo de Poblacion, ";
                    } else if (!txtMaximoPoblacion.getText().equals("")) {
                        if (isInteger(txtMaximoPoblacion.getText())) {
                            if (Integer.parseInt(txtMaximoPoblacion.getText()) < Integer.parseInt(txtMinimoPoblacion.getText())) {
                                error += "Minimo de Poblacion, ";
                            }
                        }
                    }
                } else {
                    error += "Minimo de Poblacion, ";
                }
            } else {
                error += "Minimo de Poblacion, ";
            }
            if (!txtNumeroDeHilos.getText().equals("")) {
                if (isInteger(txtNumeroDeHilos.getText())) {
                    if (Integer.parseInt(txtNumeroDeHilos.getText()) <= 0) {
                        error += "Numero de Hilos, ";
                    }
                } else {
                    error += "Numero de Hilos, ";
                }
            } else {
                error += "Numero de Hilos, ";
            }
            if (!txtTasaDeCasamiento.getText().equals("")) {
                if (isNumeric(txtTasaDeCasamiento.getText())) {
                    if (Double.parseDouble(txtTasaDeCasamiento.getText()) < 0
                            || Double.parseDouble(txtTasaDeCasamiento.getText()) > 1) {
                        error += "Tasa de Casamiento, ";
                    }
                } else {
                    error += "Tasa de Casamiento, ";
                }
            } else {
                error += "Tasa de Casamiento, ";
            }
            if (!txtTasaDeMutacion.getText().equals("")) {
                if (isNumeric(txtTasaDeMutacion.getText())) {
                    if (Double.parseDouble(txtTasaDeMutacion.getText()) < 0
                            || Double.parseDouble(txtTasaDeMutacion.getText()) > 1) {
                        error += "Tasa de Mutacion. ";
                    }
                } else {
                    error += "Tasa de Mutacion. ";
                }
            } else {
                error += "Tasa de Mutacion. ";
            }
            error = error.substring(0, error.length() - 2);
            JOptionPane.showMessageDialog(null, "Falta llenar los campos " + error + ".");
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void guardar() {
        
        parametro.setAlfa(Float.parseFloat(txtAlpha.getText()));
        parametro.setNumiteraciones(Integer.parseInt(txtNumeroIteraciones.getText()));
        
        parametro.setMaxciclossincambiar(Integer.parseInt(txtMaxCiclosSinCambiar.getText()));
        parametro.setTampoblacioninicial(Integer.parseInt(txtMinimoPoblacion.getText()));
        parametro.setTammaxpoblacion(Integer.parseInt(txtMaximoPoblacion.getText()));
        parametro.setTasacasamiento(Float.parseFloat(txtTasaDeCasamiento.getText()));
        parametro.setTasamutacion(Float.parseFloat(txtTasaDeMutacion.getText()));
        parametro.setNumhilos(Integer.parseInt(txtNumeroDeHilos.getText()));
        
        String res = ControladorProduccion.actualizarParametrosAlgoritmo(parametro);
        
        if (res == null) {
            JOptionPane.showMessageDialog(null, "Datos guardados");
        } else {
            JOptionPane.showMessageDialog(null, res);
        }
        
        cargarDatos();
    }

    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");
    }

    public static boolean isInteger(String str) {
        return str.matches("-?\\d+");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtAlpha;
    private javax.swing.JTextField txtMaxCiclosSinCambiar;
    private javax.swing.JTextField txtMaximoPoblacion;
    private javax.swing.JTextField txtMinimoPoblacion;
    private javax.swing.JTextField txtNumeroDeHilos;
    private javax.swing.JTextField txtNumeroIteraciones;
    private javax.swing.JTextField txtTasaDeCasamiento;
    private javax.swing.JTextField txtTasaDeMutacion;
    // End of variables declaration//GEN-END:variables
}
